/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of GW-Android.

GW-Android is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GW-Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with GW-Android. If not, see <http://www.gnu.org/licenses/>. */

package br.ufes.cwtools.gw.android.components;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.io.IOUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RequiredComponentsParser {
	private ArrayList<RequiredComponent> components;
	private String repository;

	public void parse(InputStream source) {
		try {
			SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
			RequiredComponentHandler handler = new RequiredComponentHandler();
			parser.parse(source, handler);
			components = handler.getComponents();
			repository = handler.getRepository();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

	public void parse(String source) {
		parse(IOUtils.toInputStream(source));
	}
	
	public void clear(){
		components = null;
		repository = null;
	}
	
	
	public ArrayList<RequiredComponent> getRequiredComponents() {
		return components;
	}

	public String getRepository(){
		return repository;
	}

	private static class RequiredComponentHandler extends DefaultHandler {
		private static final String APPLICATION = "Application";
		private static final String COMPONENT = "Component";
		
		private ArrayList<RequiredComponent> components;
		private String repository;
		
		public ArrayList<RequiredComponent> getComponents() {
			return components;
		}
		
		public String getRepository(){
			return repository;
		}
		
		@Override
		public void startDocument() throws SAXException {
			super.startDocument();
			components = new ArrayList<RequiredComponent>();
		}
		
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			super.startElement(uri, localName, qName, attributes);
			
			if (localName.equalsIgnoreCase(APPLICATION)){
				repository = attributes.getValue("repository");
			} else if (localName.equalsIgnoreCase(COMPONENT)) {
				RequiredComponent rc = new RequiredComponent();
				rc.setName(attributes.getValue("name"));
				rc.setDescription(attributes.getValue("description"));
				rc.setVersion(attributes.getValue("version"));
				try {
					rc.setSize(Long.parseLong(attributes.getValue("size")));
				} catch (NumberFormatException e) {
					rc.setSize(0);
				}
				rc.setPackageName(attributes.getValue("package"));
				rc.setMd5hash(attributes.getValue("md5hash"));
				rc.setBaseUrl(attributes.getValue("baseUrl"));
				components.add(rc);
			}
		}
	}
}

